//package filter;
//package com.neutech.filter;
//
//import com.neutech.entity.User;
//import com.neutech.server.UserServer;
//import com.neutech.server.impl.UserServerImpl;
//import com.neutech.utils.CookieUtils;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//
//@WebFilter(urlPatterns = {"/*"})
//public class LoginFilter implements Filter {
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//
//        System.out.println("******************************************");
//        System.out.println("doFilter执行了");
//        System.out.println("******************************************");
//
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        String uri = request.getRequestURI();
//        if (uri.endsWith("login.jsp") ||
//                uri.endsWith("register.jsp") ||
//                uri.endsWith("/login") ||
//                uri.endsWith("/register") ||
//                uri.endsWith(".png") ||
//                uri.endsWith(".jpg") ||
//                uri.endsWith(".js") ||
//                uri.endsWith(".css")
//        ) {
//            filterChain.doFilter(request,response);
//        } else {
//            HttpSession session = request.getSession();
//            User user = (User) session.getAttribute("user");
//            if (user == null) {
//                if (CookieUtils.cookies2Map(request.getCookies()).get("username") == null) {
//                    response.sendRedirect("/user/login");
//                } else {
//                    UserServer userServer = new UserServerImpl();
//                    User userdata = userServer.getUserByUsername(CookieUtils.cookies2Map(request.getCookies()).get("username"));
//                    if (userdata != null) {
//                        session.setAttribute("user", userdata);
//                        filterChain.doFilter(request,response);
//                    } else {
//                        response.sendRedirect("/user/login");
//                    }
//                }
//            } else {
//                filterChain.doFilter(request,response);
//            }
//        }
//    }
//}
